home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / prog / atari / c / lbbs07 / miscio.c < prev    next >
C/C++ Source or Header  |  1993-07-25  |  8KB  |  477 lines

  1. /*
  2.  *    BBS Level One - IO & Misc functions
  3.  *    (LazyBBS Project)
  4.  *
  5.  *    Public domain: may be copied and sold freely
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <time.h>
  11. #include <stdarg.h>
  12. #include <string.h>
  13. #include <ctype.h>
  14.  
  15. #include "bbs.h"
  16. #include "sysdep.h"
  17. #include "miscio.h"
  18. #include "msg.h"   /* proto for the_end() */
  19. #include "login.h" /* proto for the_end() */
  20.  
  21. /* =================================================== GLOBAL VARIABLES */
  22.  
  23. /* max download */
  24. static int maxdownload=D_MAXDOWN;
  25.  
  26. int get_maxdown(void )
  27. {
  28.     return maxdownload;
  29. }
  30.  
  31. void set_maxdown(int m)
  32. {
  33.     maxdownload=m;
  34. }
  35.  
  36. int get_newdown(void )
  37. {
  38.     return (((maxdownload/6)/10)*10); /* 1/6 max, nearest decade */
  39. }
  40.  
  41. /* endsession: end of session time */
  42. static clock_t endsession=0;
  43.  
  44. clock_t get_endsession(void)
  45. {
  46.     return endsession;
  47. }
  48.  
  49. void set_endsession(int minutes)
  50. {
  51.     endsession=clock()+(minutes*60*CLK_TCK);
  52. }
  53.  
  54. /* netarea: netmail area */
  55. static char netarea_ptr[BBSSTR];/* used by display_login and main */
  56.  
  57. char *get_netarea(void)
  58. {
  59.     return (char *)netarea_ptr;    
  60. }
  61.  
  62. void set_netarea(char *net)
  63. {
  64.     strcpy(netarea_ptr,net);
  65. }
  66.  
  67. /* god: sysop with privileges */
  68. int godflag=0;
  69.  
  70. /* Set god (sysop) status */
  71.  
  72. void setgod(void )
  73. {
  74.     godflag++;
  75. }
  76.  
  77. /* Get god flag */
  78.  
  79. int isgod(void )
  80. {
  81.     return godflag;
  82. }
  83.  
  84. /* tempfile: return temporary file name (*must* be the same each time) */
  85. static char ntempfile[BBSSTR];
  86.  
  87. char *tempfile(void )
  88. {
  89.     sprintf(ntempfile,"lbbs.%d",getpid());
  90.     return ntempfile;
  91. }
  92.  
  93. /* ============================================================== DATE */
  94.  
  95. /*
  96.  *    today: returns the nb of days since 1/1/70
  97.  */
  98.  
  99. int today(void)
  100. {
  101.     long t;
  102.     t=(long)timenix(NULL)/86400;
  103.     return (int)t;
  104. }
  105.  
  106. /* ==================================================== EMERGENCY EXIT */
  107.  
  108. void the_end()
  109. {
  110.     logline(3,"Exiting BBS");
  111.     close_areas();
  112.     close_login();
  113.     close_the_log();
  114.     hangup();
  115.     exit(0);
  116. }
  117.  
  118. /* ==================================================== INPUT ROUTINES */
  119.  
  120. int dowatchcarrier=0;
  121. int passwd_echo=0;
  122.  
  123. /*
  124.  *    getstring: input a string from user of mxlenght==BBSSTR
  125.  */
  126.  
  127. int getpasswdstring(char *str)
  128. {
  129.     int ret;
  130.     
  131.     passwd_echo=1; /* bahhhhhhh, awful :-) */
  132.     ret=getstring(str);
  133.     passwd_echo=0;
  134.     return ret;
  135. }
  136.  
  137. int getstring(char *s)
  138. {
  139.     int pos,key;
  140.  
  141.     sysdep_flushin();
  142.     fflush(stdout);
  143.     
  144.     pos=0;
  145.     while(1)
  146.     {
  147.         key=getkey();
  148.     
  149.         if(key<0)
  150.         {
  151.             *s=0;
  152.             return BBSFAIL;
  153.         }
  154.         else if(key==10 || key==13) /* return cr|lf */
  155.         {
  156.             s[pos++]=0;
  157.             return BBSOK;
  158.         }
  159.         else if(key==8)    /* backspace */
  160.         {
  161.             if(pos>0)
  162.             {
  163.                 pos--;
  164.                 out_char(' '); out_char(8);/* delete char */
  165.                 if(ECHOCHAR) out_char(key);
  166.             }
  167.         }    
  168.         else if(isokchar(key) && (pos<BBSSTR-2))
  169.         {
  170.             s[pos++]=(char) key;
  171.             if(ECHOCHAR) out_char(key);
  172.         }
  173.         else
  174.             out_char(7); /* bip */
  175.     }
  176. }
  177.  
  178. int isokchar(int c)
  179. {
  180.     if(c>=0x20 && c<127)
  181.         return 1;
  182.     return 0;
  183. }
  184.  
  185. void enable_watch(void)
  186. {
  187.     dowatchcarrier++;
  188. }
  189.  
  190. int carrier(void )
  191. {
  192.     if(!dowatchcarrier)
  193.         return 1;
  194.     return !(sysdep_carrier());    
  195. }
  196.  
  197. int getkey(void) /* get a key */
  198. {
  199.     unsigned char mychar;
  200.     int n;
  201.     clock_t end;
  202.     
  203.     fflush(stdout);
  204.     
  205.     end=clock()+(CLK_TCK*S_TIMEOUT);
  206.     
  207.     do {
  208.         n=sysdep_getchar(&mychar);
  209.     } while(n==BBSFAIL && clock()<end && carrier());
  210.     
  211.     mychar&=0x7F; /* strip 8th bit */
  212.     
  213.     if(n==BBSFAIL)
  214.     {
  215.         out_printf("Timeout or carrier loss!!!\n");
  216.         logline(1,"User didn't react / carrier lost");
  217.         the_end();
  218.     }
  219.     
  220.     if(clock()>get_endsession())
  221.     {
  222.         out_printf("End of session time, bye!");
  223.         logline(1,"End of session time");
  224.         the_end(); /* => hangup */
  225.     }
  226.     
  227.     if(!carrier())
  228.     {
  229.         logline(1,"Lost carrier");
  230.         the_end();
  231.     }
  232.     
  233.     if(mychar==4)
  234.     {
  235.         logline(1,"User quit with control-d");
  236.         the_end();
  237.     }
  238.     
  239. #ifdef DO_ECHO_MYSELF
  240.     /* don't echo ESC and password chars */
  241.     if(!passwd_echo && mychar!=0x1B)
  242.         out_char(mychar);
  243.     else
  244.         out_char('.');
  245. #endif
  246.  
  247.     return (int) mychar;
  248. }
  249.  
  250. void hangup(void )
  251. {
  252.     if(dowatchcarrier)
  253.         sysdep_hangup();
  254. }
  255.  
  256. /* ================================================== OUTPUT ROUTINES */
  257.  
  258. void out_printf(char *line, ...)
  259. {
  260.     char    temp[1000];
  261.     va_list param;
  262.     
  263.     /* process */
  264.     va_start(param, line);
  265.     vsprintf(temp, line, param);
  266.     va_end(param);
  267.     
  268.     out_string(temp);
  269. }
  270.  
  271. void out_string(unsigned char *s)
  272. {
  273.     while(*s)
  274.     {
  275.         out_char(*s);
  276.         s++;
  277.     }
  278. }
  279.  
  280. void out_char(unsigned char s)
  281. {
  282.     if(s=='\n')    /* \n to crlf */
  283.         sysdep_putchar('\r');
  284.  
  285.     sysdep_putchar(s);
  286. }
  287.  
  288. /* =================================================== STRING PARSING */
  289.  
  290. /*
  291.  * Insert one space at the beginning of a string
  292.  */
  293.  
  294. void strspins(char *str)
  295. {
  296.     int maxlen,i;
  297.     
  298.     maxlen=strlen(str);
  299.     
  300.     for(i=maxlen+1;i>=0;i--)
  301.         str[i+1]=str[i];
  302.     str[0]=' ';
  303. }
  304.  
  305. /*
  306.  *    file function that makes a path from ambigous string
  307.  */
  308.  
  309. void addslash(char *s)
  310. {
  311.     if(*s)
  312.     {
  313.         if(s[strlen(s)-1]!=SYSSEPAR)
  314.             strcat(s,SYSSTRSEPAR);
  315.     }
  316. }
  317.  
  318. /* 
  319.  *    findword: put in _copy_ the start of _string_ before 0 or '@' 
  320.  *
  321.  *    returns NULL if endofstr or next pointer
  322.  */
  323.  
  324. char *findword(char *copy, char *string)
  325. {
  326.     int len=0; /* nb of chars to copy */
  327.     
  328.     if(!string)
  329.         return NULL;
  330.         
  331.     while(string[len])
  332.     {
  333.         if(string[len]==BBSSEPAR) 
  334.             break;
  335.         len++;
  336.     }
  337.     
  338.     if(len<BBSSTR)
  339.     {
  340.         strncpy(copy,string,len);
  341.         copy[len]=0;
  342.     }
  343.     else
  344.         strcpy(copy,"string too long");
  345.     
  346.     if(string[len]==0)
  347.         return NULL;
  348.     else
  349.         return string+len+1;
  350. }
  351.  
  352. /*
  353.  *    iscomment: is this char a comment?
  354.  */
  355.  
  356. int iscomment(char c)
  357. {
  358.     switch(c)
  359.     {
  360.         case '#':
  361.         case ';':
  362.         return 1;
  363.     }
  364.     return 0;
  365. }
  366.  
  367. /*
  368.  *    strcln: remove a char from string
  369.  */
  370.  
  371. void strcln(char *string, char c)
  372. {
  373.     char *s=string;
  374.     while(*s)
  375.     {
  376.         if((c!=-1 && *s==c) || (c==-1 && *s<0x20 && *s>0))
  377.             strcpy(s,s+1);
  378.         s++;
  379.     }
  380. }
  381.  
  382. /* find next string format "plouf <tab>tralala ; comment */
  383.  
  384. char *nextstr(char *str)
  385. {
  386.     char* s=str;
  387.     
  388.     while((*s!='\0') && (*s!=' ') && (*s!='\x09'))
  389.         s++;
  390.         
  391.     if(*s=='\0')
  392.         return NULL;
  393.  
  394.     while((*s==' ') || (*s=='\x09'))
  395.         s++;
  396.     
  397.     if((*s=='\0') || (*s==';'))
  398.         return NULL;
  399.         
  400.     return s;
  401. }
  402.  
  403. /* copy string until space */
  404.  
  405. void strspacecpy(char *dest, char *srce)
  406. {
  407.     int i=0,j=0;
  408.     
  409.     while((srce[i]!='\0') && (srce[i]!=' ') && (srce[i]!='\x09') && (i<BBSSTR))
  410.     {
  411. /* fixme: unsigned  char ? */
  412.         if(srce[i]>' ')
  413.         {
  414.             dest[j]=srce[i];
  415.             j++;
  416.         }
  417.         i++;
  418.     } 
  419.     dest[i]='\0';
  420. }
  421.  
  422. /* ================================================= LOG FILE FUNCTIONS */
  423.  
  424. FILE *logfile=NULL;
  425. int loglevel=0;
  426.  
  427. /* logline: Log a line in the logfile and output to screen */
  428.  
  429. void logline (int level, char *line, ...)
  430. {
  431.     time_t    timer;
  432.     char    temp[100], out[100],tdate[20];
  433.     va_list param;
  434.     struct tm *tim;
  435.     
  436.     char ls[]=" -+*=!";
  437.     
  438.     if(!logfile)
  439.         return;    /* error */
  440.         
  441.     /* process */
  442.     va_start(param, line);
  443.     vsprintf(temp, line, param);
  444.     va_end(param);
  445.     time (&timer);
  446.     tim = localtime (&timer);
  447.     strftime (tdate, 20, "%d %b %H:%M:%S", tim);
  448.     sprintf (out, "%c %s LBBS %s\n", ls[level], tdate, temp);
  449.  
  450.     /* log to file */
  451.     fputs(out,logfile);
  452. #ifdef DEBUG
  453.     fflush(logfile);
  454. #endif
  455. }
  456.  
  457. /* openlog: create logfile */
  458.  
  459. int open_the_log(char *nm, int level)
  460. {
  461.     loglevel=level;
  462.     logfile=fopen(nm,"a");
  463.     if(logfile)
  464.         return BBSOK;
  465.     
  466.     printf("\0x7\0x7Can't open logfile!\n");
  467.     return BBSFAIL;
  468. }
  469.  
  470. /* closelog: close logfile */
  471.  
  472. void close_the_log(void )
  473. {
  474.     if(logfile)
  475.         fclose(logfile);
  476. }
  477.